package com.nanfang51g3.ckz.ui.money.add;

import android.text.TextUtils;

import com.nanfang51g3.ckz.di.ApiModule;
import com.nanfang51g3.ckz.domain.HttpResultBean;
import com.nanfang51g3.ckz.domain.UserBean;
import com.nanfang51g3.ckz.exec.Api2Exception;
import com.nanfang51g3.ckz.http.HttpResultFunc;
import com.nanfang51g3.ckz.http.RestApi;
import com.nanfang51g3.ckz.http.SubscriberInterface;
import com.nanfang51g3.ckz.utils.KLog;
import com.nanfang51g3.ckz.utils.MD5Utils;
import com.nanfang51g3.ckz.utils.SPUtil;
import com.nanfang51g3.ckz.utils.ToolUtils;

import java.util.TreeMap;

import javax.inject.Inject;

import retrofit2.Retrofit;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2016/4/13.
 */
public class AddFinancialInfoPresenter implements AddFinancialInfoContract.AddFinancialInfoActionsListener {
    String message;
    private final RestApi _restApi;
    private final AddFinancialInfoContract.View mView;

    @Inject
    public AddFinancialInfoPresenter(RestApi restApi, AddFinancialInfoContract.View mView) {
        KLog.i("TAG", "进入了...AddFinancialInfoPresenter");
        /**
         * @see ApiModule#provideRestApi(Retrofit)
         */
        this._restApi = restApi;
        KLog.i("TAG", _restApi == null ? "进入了...is null" : "进入了...not null");
        /**
         * @see AddFinancialInfoPresenterModule#AddFinancialInfoPresenterModule(AddFinancialInfoContract.View)
         */
        this.mView = mView;
        KLog.i("TAG", mView == null ? "进入了...is null" : "进入了...not null");
    }

    @Override
    public void addRelName(String relName) {
        if (TextUtils.isEmpty(relName)) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("姓名不能为空");
            return;
        }
        if (!ToolUtils.isLetterDigitOrChinese(relName)) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("请输入正确的姓名");
            return;
        }
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("real_name", relName);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoRealName(userid, relName, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new AddAliPayResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);


                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "real_name", o.getReal_name());
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see AddFinancialInfoActivity#finalView()
                         */
                        mView.finalView();
                    }
                });
    }

    @Override
    public void addBankName(String bankName) {
        if (TextUtils.isEmpty(bankName)) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("银行名称不能空");
            return;
        }
        if (!bankName.contains("银行")) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("请输入正确的银行名称");
            return;
        }
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("bank", bankName);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoBankName(userid, bankName, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new AddAliPayResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "bank_name", o.getBank());
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see  AddFinancialInfoActivity#finalView()
                         */
                        mView.finalView();
                    }
                });
    }

    @Override
    public void addBankCard(String bankCard) {
        if (TextUtils.isEmpty(bankCard)) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("银行卡号不能为空");
            return;
        }
        if (!ToolUtils.checkBankCard(bankCard)) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("请输入正确的银行卡号");
            return;
        }
        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("bank_card", bankCard);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoBankCard(userid, bankCard, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new AddAliPayResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "bank_card", o.getBank_card());
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see AddFinancialInfoActivity#finalView()
                         */
                        mView.finalView();
                    }
                });
    }

    @Override
    public void addAliPay(String aliPay) {
        if (TextUtils.isEmpty(aliPay)) {
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("支付宝号不能为空");
            return;
        }
        KLog.i("Evd", "no = " + ToolUtils.isMobileNO(aliPay) + " em = " + ToolUtils.isValidEmail(aliPay));
        if (!ToolUtils.isMobileNO(aliPay) && !ToolUtils.isValidEmail(aliPay)) {
            KLog.i("");
            /**
             * @see AddFinancialInfoActivity#showMessage(String)
             */
            mView.showMessage("请输入正确的支付宝号");
            return;

        }

        String userid = (String) SPUtil.get(mView.getContext(), "userid", "");
        TreeMap<String, String> treeMap = new TreeMap<>();
        treeMap.put("userid", userid);
        treeMap.put("alipay", aliPay);
        String sign = MD5Utils.getAccessToken(treeMap);
        KLog.i("Evd", treeMap.toString() + "-" + sign);
        Observable observable = _restApi.reuserinfoAliPay(userid, aliPay, sign);
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new AddAliPayResultFunc<UserBean>())
                .subscribe(new SubscriberInterface<UserBean>() {
                    @Override
                    public void onCompleted() {
                        KLog.i("Evd", "onCompleted");

                    }

                    @Override
                    public void _onError(String msg) {
                        KLog.i("Evd", "onError " + msg);
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(msg);
                    }

                    @Override
                    public void onNext(UserBean o) {
                        KLog.i("Evd", "onNext " + o.toString());
                        SPUtil.putAndApply(mView.getContext(), "ali_pay_card", o.getAlipay());
                        /**
                         * @see AddFinancialInfoActivity#showMessage(String)
                         */
                        mView.showMessage(message);
                        /**
                         * @see AddFinancialInfoActivity#finalView()
                         */
                        mView.finalView();
                    }
                });
    }

    class AddAliPayResultFunc<T> extends HttpResultFunc<T> {
        @Override
        public T call(HttpResultBean<T> httpResult) {
            KLog.i("Evd", httpResult == null ? "1-" : "0-" + httpResult.getCode() + "-" + httpResult.getInfo() + "-" + httpResult.getData().toString());
            if (httpResult.getCode() == 2 ||
                    httpResult.getCode() == 3 ||
                    httpResult.getCode() == 4 ||
                    httpResult.getCode() == 5 ||
                    httpResult.getCode() == 6 ||
                    httpResult.getCode() == 7 ||
                    httpResult.getCode() == 8 ||
                    httpResult.getCode() == 9 ||
                    httpResult.getCode() == 10 ||
                    httpResult.getCode() == 11) {
                throw new Api2Exception(httpResult.getInfo());
            }
            message = httpResult.getInfo();
            return httpResult.getData();
        }
    }
}
